<?php
include("header.php");

print "<center><h1>Login to existing account</h1></center>\n";
print "<center>\n";

$cmd = $_REQUEST["submit"];
$username = $_REQUEST["username"];
$password = $_REQUEST["password"];

if($cmd=="Login"){
	LoginUser();
}else{
	PrintForm();
}

print "</center>\n";
include("footer.php");

//---------------------------
// PrintForm
//---------------------------
function PrintForm()
{
	global $username, $password;

	?>
	
	<table cellpadding="15" fillcolor="black" border="1"><tr><td>
	
	<form method="POST">
	<table border="0" cellpadding="10">
		<tr>
			<td><input type="entry" size="12" name="username" value="<?=$username?>"></td>
			<td>username *</td>
		</tr>
		<tr>
			<td><input type="password" size="12" name="password" value="<?=$password?>"></td>
			<td>Password *</td>
		</tr>
		<tr>
			<td colspan="2"><center><input type="submit" name="submit" value="Login"></center></td>
		</tr>
		<tr>
			<td colspan="2"><center><font size="-1"><i><a href="forgot_password.php">Forgotten Password?</a></center></td>
		</tr>
	</table>
	</form>
	
	</td></tr></table>
	
	<?php
}

//---------------------------
// LoginUser
//---------------------------
function LoginUser()
{
	global $username, $password;

	// Check if a user with this username exists
	if(AuthenticateUser($username, $password)){
		// session variable "userid" should be set now so just reload the home page
		?>
		<META HTTP-EQUIV="refresh" CONTENT="2; URL=index.php">
		<p><font size="+1">Authentication Succeeded!</font></p>
		<p><a href="index.php">continue</a> (automatic in 2 seconds)</p>
		<?php
	}else{
		print "<p><font size=\"+1\" color=\"red\">Authentication Failure!</font></p>\n";
		PrintForm();
	}
}

?>